<template>
  <div class="form-container">
    <el-form inline :model="form" ref="refForm" class="form-line" label-width="70">
      <el-form-item label="APP类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择APP类型">
          <el-option label="攸诺城配" value="1"></el-option>
          <el-option label="攸诺运管" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作系统" prop="platform">
        <el-select v-model="form.platform" placeholder="请选择操作系统">
          <el-option label="IOS" value="3"></el-option>
          <el-option label="Android" value="4"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <el-button type="primary" @click="handleReset(refForm as FormInstance)">重置</el-button>
      <el-button type="primary" @click="getAppList">查询</el-button>
      <el-button type="primary" @click="router.push('/provinceLevel/proBasic/proAPP/add')">上传APP</el-button>
    </div>
  </div>
  <div class="table-container">
    <el-table
      border
      :cell-style="{ textAlign: 'center', fontSize: '13px' }"
      :header-cell-style="{
        textAlign: 'center',
        fontWeight: '100',
        fontSize: '13px',
        color: 'white',
        backgroundColor: '#009688'
      }"
      style="width: 100%"
      :data="tableData"
    >
      <el-table-column fixed prop="appType" label="APP类型">
        <template #default="scope">
          <el-tag>{{ scope.row.appType == "1" ? "攸诺城配" : "攸诺运管" }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="version" label="版本名" />
      <el-table-column prop="platform" label="操作系统">
        <template #default="scope">
          <el-tag>{{ scope.row.platform == "3" ? "IOS" : "Android" }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isForceUpdate" label="是否强制">
        <template #default="scope">
          <el-tag>{{ scope.row.isForceUpdate ? "是" : "否" }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="上传时间" />
      <el-table-column prop="releaseDate" label="上架时间" />
      <el-table-column prop="status" label="上架状态">
        <template #default="scope">
          <el-switch v-model="scope.row.status" @change="handleStatus(scope.row)" active-value="2" inactive-value="1"></el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button
            link
            type="primary"
            size="small"
            @click="router.push({ path: '/provinceLevel/proBasic/proAPP/detail', query: { id: scope.row.id } })"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        size="small"
        layout="prev, pager, next"
        @change="getAppList"
        :total="pageTotal"
        :page-size="form.pageSize"
        v-model:current-page="form.pageNum"
      />
    </div>
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { App } from "@/api/interface/province/base";
import { getAppListAPI, updateAppStatusAPI } from "@/api/modules/province/baseAPI";
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
const pageTotal = ref<number>(0);
const form = ref<{
  platform: string;
  type: string;
  pageSize: number;
  pageNum: number;
}>({
  platform: "",
  type: "",
  pageSize: 10,
  pageNum: 1
});
const refForm = ref<FormInstance>();
const tableData = ref<App.Row[]>([]);
const handleReset = (formEl: FormInstance) => {
  formEl.resetFields();
};
const handleStatus = (row: App.Row) => {
  ElMessageBox.confirm(`确定${row.status == "1" ? "下架" : "上架"}该APP吗？`)
    .then(async () => {
      const { code } = await updateAppStatusAPI({ id: row.id, status: row.status as "1" | "2" });
      if (code == "200") {
        ElMessage.success("操作成功");
      } else {
        ElMessage.error("操作失败");
      }
      getAppList();
    })
    .catch(() => {
      ElMessage.info("已取消");
      getAppList();
    });
};
const getAppList = async () => {
  const { code, rows, total } = await getAppListAPI(form.value);
  if (code == "200" && rows) {
    if (rows) {
      tableData.value = rows;
    } else {
      tableData.value = [];
    }
    if (total) {
      pageTotal.value = total;
    }
  }
};
onMounted(() => {
  getAppList();
});
</script>
<style lang="scss" scoped>
.form-container {
  padding: 10px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
    ::v-deep .el-button > span {
      font-size: 13px;
    }
  }
  .el-form {
    display: inline-block;
    ::v-deep .el-form-item__label {
      font-size: 13px;
    }
  }
  .form-line {
    .el-form-item {
      margin: 0;
      margin-right: 20px;
    }
    .el-input {
      width: 240px;
    }
    .el-select {
      width: 240px;
    }
  }
}
.table-container {
  padding: 40px 10px;
  padding-bottom: 60px;
  background-color: white;
  .pagination-container {
    // display: inline-block;
    float: right;
    margin-top: 20px;
    background-color: white;
  }
}
</style>
